<?php

class dashboardActions extends sfActions {

    public function preExecute(){
        if (!$this->getUser()->hasAttribute('scountry')){
            if ($this->getUser()->isFirstRequest()){
                $culture = $this->getRequest()->getPreferredCulture(array('en', 'es'));
                $this->getUser()->setCulture($culture);
                $this->getUser()->isFirstRequest(false);
                $country = CountryPeer::retrieveByPK(169);/* 169: ID Perú en BD */
                $this->getUser()->setAttribute('scountry',$country);
            }else{
                $culture = $this->getUser()->getCulture();
            }
        }
    }

    public function executeIndex(sfWebRequest $request){
        $invitation = false;
        $c = new Criteria();
        $c->add(InvitationPeer::INVITED_ID, $this->getUser()->getAttribute('suid'));
        $c->add(InvitationPeer::STATE, 1);
        $invitation = InvitationPeer::doSelectOne($c);
        if(is_object($invitation)){
            $invitation = true;
        }
        $this->invitation = $invitation;
    }

    public function executeArticles(sfWebRequest $request){
        $c = new Criteria();
        $c->add(ArticlePeer::OWNER_ID, $this->getUser()->getAttribute('suid'));
        $c->add(ArticlePeer::IS_DRAFT, 0);
        $c->add(ArticlePeer::ENABLED, 1);
        $this->articles = ArticlePeer::doSelect($c);

        $c->clear();
        $c->add(ArticlePeer::OWNER_ID, $this->getUser()->getAttribute('suid'));
        $c->add(ArticlePeer::IS_DRAFT, 1);
        $c->add(ArticlePeer::ENABLED, 1);
        $this->draft_articles = ArticlePeer::doSelect($c);
    }

    public function executeHistory(sfWebRequest $request){
        $c = new Criteria();
        $c->add(HistoryPeer::USER_ID, $this->getUser()->getAttribute('suid'));
        $c->add(ArticlePeer::IS_DRAFT, 0);
        $c->addDescendingOrderByColumn(HistoryPeer::CREATED_AT);
        $c->addJoin(HistoryPeer::ARTICLE_ID, ArticlePeer::ID);
        $this->items = HistoryPeer::doSelect($c);
    }

    public function executeFavorites(sfWebRequest $request){
        $c = new Criteria();
        $c->add(CountryPeer::ID, $this->getUser()->getAttribute('scountry')->getId());
        $c->add(DetCountrySubcategoryPeer::IS_SHOWN, 1);
        $c->addJoin(DetCountrySubcategoryPeer::SUBCATEGORY_ID, SubcategoryPeer::ID);
        $c->addJoin(DetCountrySubcategoryPeer::COUNTRY_ID, CountryPeer::ID);
        $c->addAscendingOrderByColumn(SubcategoryI18nPeer::DESCRIPTION);
        $this->subcategories = SubcategoryPeer::doSelectWithI18n($c);

        $array_suattr = $this->getUser()->getAttribute('suattr');
        $this->interests = $array_suattr[5];
        $this->favArticles = $array_suattr[7];
    }

    public function executeDeleteFavorites(sfWebRequest $request){
        $id = $request->getParameter('id');/* article_id */
        /* myFavoriteArticles */
        $array_suattr = $this->getUser()->getAttribute('suattr');
        $array_favArticles = preg_split('/[*]/', $array_suattr[7]);
        $index = array_search($id, $array_favArticles);
        unset($array_favArticles[$index]);
        $array_favArticles = array_values($array_favArticles);
        $string = "";
        $count = 0;
        foreach($array_favArticles as $fa){
            if($count==0){
                $string .= $fa;
            }else{
                $string .= "*".$fa;
            }
            $count++;
        }
        $logged_user = AccountPeer::retrieveByPK($this->getUser()->getAttribute('suid'));
        $logged_user->setFavoriteArticles($string);
        $logged_user->save();
        $array_suattr[7] = $string;
        $this->getUser()->setAttribute('suattr',$array_suattr);

        $this->redirect('@myFavorites');
    }

    public function executeBalance(sfWebRequest $request){
        //
    }

    public function executeChangeInterests(sfWebRequest $request){
        $interests = $request->getParameter('interests');
        $string = "";
        $count = 0;
        foreach($interests as $interest){
            if($count==0){
                $string .= $interest;
            }else{
                $string .= "*".$interest;
            }
            $count++;
        }
        $suid = $this->getUser()->getAttribute('suid');
        $object = AccountPeer::retrieveByPK($suid);
        $object->setInterests($string);
        $object->save();
        $array_suattr = $this->getUser()->getAttribute('suattr');
        $array_suattr[5] = $string;
        $this->getUser()->setAttribute('suattr',$array_suattr);
        $this->redirect('@myFavorites');
    }

}